<?php

namespace App\Models;

class RoleNode extends Model
{
	private static $_obj;
	
	public static function getInstance()
	{
		if (!self::$_obj instanceof self) {
			self::$_obj = new self;
		}
		return self::$_obj;
	}

	protected $table = 'role_node';

	public $timestamps = false;

	protected $guarded = [];

	/**
	 * 添加节点角色关联操作
	 */
	public function addRoleNode(array $arr, $rid)
	{
		if (!$arr || !$rid) {
			return false;
		}
		$data = [];
		foreach ($arr as $key => $val) {
			$data[] = [
				'rid'=>$rid,
				'nid'=>$val,
			];
		}
		$result = $this->insert($data);
		return $result;
	}

	/**
	 * 根据角色id删除关联表
	 */
	public function delRoleNodeByRid($id)
	{
		$res = self::where('rid',$id)->delete();
		return $res;
	}

	/**
	 * 根据节点/权限id删除关联表,用于节点/权限更改状态和删除时的操作
	 */
	public function delRoleNodeByNid($id)
	{
		$res = self::where("nid",$id)->delete();
		return $res;
	}

	/**
	 * 根据角色id获取关联的节点/权限id
	 */
	public function getNodeData($id)
	{
		$data = self::where([["rid",$id],['status',1]])->get();
		$res = [];
		foreach ($data as $key => $val) {
			$res[] = $val->nid;
		}
		return $res;
	}
}